package com.aaa.mrcai.service.impl;


import com.aaa.mrcai.mapper.EmpMapper;
import com.aaa.mrcai.pojo.entity.Dept;
import com.aaa.mrcai.pojo.entity.Emp;
import com.aaa.mrcai.pojo.entity.Role;
import com.aaa.mrcai.pojo.vo.EmpVo;
import com.aaa.mrcai.query.EmpQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author meowyf
 * @version 1.0
 * @Name Z_MJ
 * @date 2021/12/02 19:38:57
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private HttpSession session;
    @Override
    public AjaxResult list(EmpQuery empQuery) {
        List<Emp> i = empMapper.list(empQuery);

        Long totalCount = empMapper.queryEmpTotalCount(empQuery);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("empListAll",i);
        map.put("empNumber",totalCount);

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult AddEmp(EmpVo empVo) {

        Long empExists = empMapper.isEmpExists(empVo.getEname(), empVo.getTelephone());
        System.out.println("2"+empExists);
        if (empExists>0){
            return AjaxResult.defeated("该部门已经该员工了 请核对后添加");
        }
        int i = empMapper.addEmp(empVo);
        empVo.setAdminname(empVo.getTelephone());
        empVo.setPassword(empVo.getTelephone().substring(5));
        System.out.println("3"+i);
        if (i>0){
            int adduser = empMapper.addUser(empVo);
            if (adduser>0){
                return AjaxResult.success("添加成功");
            }
        }
        return AjaxResult.defeated("添加失败");

    }

    @Override
    public AjaxResult getEmp(int id) {
        EmpVo e = empMapper.getEmp(id);
        session.setAttribute("getEmpByID",e);
        return AjaxResult.success(e);
    }

    @Override
    public AjaxResult UpdateEmp(EmpVo empVo) {
        Long upEmpExists = empMapper.isUpEmpExists(empVo);
        if (upEmpExists>0){
            return AjaxResult.defeated("该部门已经该员工了 请核对后添加");
        }
        int i = empMapper.updateEmp(empVo);
        if (i==0){
            return AjaxResult.defeated("修改失败");
        }

        return AjaxResult.success("修改成功");
    }

    @Override
    public AjaxResult deleteEmp(int id) {
        int i = empMapper.deleteEmp(id);
        if (i>0){
            int deleteuser = empMapper.deleteUser(id);
            if (deleteuser>0){
                return AjaxResult.success();
            }
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult listDept() {
        List<Dept> depts = empMapper.listDept();
        System.out.println(depts+"123456");
        return AjaxResult.success(depts);
    }

    @Override
    public AjaxResult listrole() {
         List<Role> roles =  empMapper.listRole();
        return AjaxResult.success(roles);
    }

}
